<?php
namespace App\Service;
use App\Models\Admin\Member as MemberModel;
use App\Models\Admin\UserProfile as UserProfileModel;
use Illuminate\Support\Facades\DB;
use App\Models\UserBind as UserBindModel;

class MemberService extends BaseService{
    protected $modelName = "Admin.Member";

    public function getMemberList($limit,$search){
        $org_ids = (new BasicService())->getOrgIds();
        return  MemberModel::
            join('post as p','user.post_id','p.id')
            ->join('org as o','user.org_id','o.id')
            ->leftjoin('user_profile as u','user.id','u.id')
            ->select('user.*','p.name as post_name','o.name as org_name','u.truename')
            ->where(function($query) use ($search){
                if(!empty($search['username'])){
                    $query->where("user.username","like","%".$search['username']."%");
                }
                if(!empty($search['truename'])){
                    $query->where("u.truename","like","%".$search['truename']."%");
                }
                if(!empty($search['post_id'])){
                    $query->where("post_id",$search['post_id']);
                }
                if(!empty($search['org_index_code'])){
                    $query->where("org_code","like",$search['org_index_code']."%");
                }
                if(isset($search['is_locked'])){
                    if($search['is_locked'] != ""){
                        $query->where("is_locked",$search['is_locked']);
                    }
                }
            })
            ->whereIn('org_id',$org_ids)
            ->orderBy('id','desc')->paginate($limit)->toArray();
    }
    public function delete($ids) {
        DB::beginTransaction();
        $user = MemberModel::where(function($query) use ($ids){
            if(is_array($ids)){
                $query->whereIn('id',$ids);
            }else{
                $query->where('id',$ids);
            }})->delete();
        $count = UserProfileModel::where(function($query) use ($ids){
            if(is_array($ids)){
                $query->whereIn('id',$ids);
            }else{
                $query->where('id',$ids);
            }})->count();
        if($count > 0){
            $profile = UserProfileModel::where(function($query) use ($ids){
                if(is_array($ids)){
                    $query->whereIn('id',$ids);
                }else{
                    $query->where('id',$ids);
                }})->delete();
        }else{
            $profile = true;
        }
        if($user && $profile){
            DB::commit();
            return 1;
        }else{
            DB::rollBack();
            return -1;
        }
    }
    public function create($data){
        DB::beginTransaction();
        $res1 = MemberModel::create($data)->id;
        $arr['id'] = $res1;
        $arr['truename'] = $data['truename'];
        $res2 = UserProfileModel::create($arr);
        if($res1 && $res2){
            DB::commit();
            return 1;
        }else{
            DB::rollBack();
            return -1;
        }
    }
    public function selectOpenid($where){
        return MemberModel::withoutGlobalScope('site_id_scope')->where($where)->get()->toArray();
    }
    public function createBind($data){
        return UserBindModel::create($data);
    }
}
